<template>
	<view class="container">
		<view class="item">
			<view class="goods">
				<view class="goods-item" @click="toDetail(item.product_id)" v-for="(item, index) in list" :key="index">
					<image class="goods-image" :src="item.product.post_url"></image>
					<view class="goods-title">
						{{item.product.title}}
					</view>
					<view class="goods-tag">
            <text  class="goods-tag-text" v-for="(i, index) in (item.product.tags ? item.product.tags.split(',') : [])" :key="index">
              {{i}}
            </text>
					</view>
					<view class="goods-price">
						<text class="goods-price-txt">￥{{item.product.price}}<text class="goods-price-unit">
								/{{item.product.unit || ''}}</text></text>
						<image @click="addCart(item.product.id)" class="goods-shopcar-icon" src="/static/icon/shopcar.svg"></image>
					</view>
				</view>
			</view>
		</view>
		<!-- 内容为空 -->
		<view class="empty" v-if="list.length == 0">
			<u-empty text="抱歉，您还没有收藏商品" mode="favor"></u-empty>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				list: [
				],
        page: 1
			}
		},
    onShow() {
      this.getProductList()
    },
    methods: {
      getProductList() {
        if (!this.$store.state.userInfo.id) return
        this.$api.getCollectionList({
          pageNum: this.page,
          pageSize: 20,
          user_id: this.$store.state.userInfo.id
        }).then(res => {
          this.list = res.rows.map(item => {
            return {
              ...item,
              product: item.product || {}
            }
          })
        })
      },
      toDetail(id) {
        uni.navigateTo({
          url: '/pages/goods/goods?id=' + id
        })
      },
      addCart(id) {
        uni.showLoading()
        this.$api.addCart({
          product_id: id,
          user_id: this.$store.state.userInfo.id
        }).then(res => {
          this.$utils.toastTip("成功加入购物车");
        })
      },
    }
	}
</script>

<style>
	page {
		background: #F8F9FA;
	}
</style>

<style lang="scss" scoped>
	.container {
		margin: 30rpx;
	}

	.item {
		padding-bottom: 60rpx;

		.item-lable {
			font-size: 28rpx;
			color: #666666;
		}

		.goods {
			display: flex;
			flex-wrap: wrap;
			justify-content: space-between;

			.goods-item {
				border-radius: 20rpx;
				background: #FFFFFF;
				padding: 0rpx 30rpx 30rpx 30rpx;
				margin-bottom: 30rpx;
			}

			.goods-image {
				width: 280rpx;
				height: 280rpx;
				border-radius: 4px;
			}

			.goods-title {
				width: 274rpx;
				font-size: 26rpx;
				font-weight: 700;
				line-height: 34rpx;
				color: #333333;
				margin-top: 10rpx;
			}

			.goods-tag {
				display: flex;
				align-items: center;
				margin-top: 14rpx;
			}

			.goods-tag-owner {
				color: #F55726;
				font-size: 18rpx;
				border: 1px solid #F55726;
				border-radius: 4rpx;
				padding: 2rpx 10rpx;
				line-height: 22rpx;
			}

			.goods-tag-text {
				color: #40AE36;
				font-size: 18rpx;
				border: 1px solid #40AE36;
				border-radius: 4rpx;
				margin-left: 10rpx;
				padding: 2rpx 10rpx;
				line-height: 22rpx;
			}

			.goods-price {
				display: flex;
				align-items: center;
				justify-content: space-between;
				margin-top: 14rpx;

				.goods-price-txt {
					color: #F55726;
					font-size: 32rpx;
					font-weight: 600;

					.goods-price-unit {
						font-size: 22rpx;
						color: #999999;
						font-weight: normal;
					}
				}

				.goods-shopcar-icon {
					width: 44rpx;
					height: 44rpx;
					margin-right: 10rpx;
				}
			}
		}
	}

	.empty {
		margin-top: 200rpx;
	}
</style>
